<!-- 在塘沽产（吊水） -->
<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList(), searchPage()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">选择鱼塘</div>
            <el-select filterable clearable v-model="fishPondId" filterable clearable @change="() => {
              productionBatchId = ''
            }">
              <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName" :value="item.fishPondId" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">批次编号</div>
            <el-select filterable clearable v-model="productionBatchId" filterable clearable>
              <template v-for="(item, index) in batchList">
                <el-option v-if="!fishPondId || item.fishPondId == fishPondId" :key="index"
                  :label="`${item.mnecode}_${item.varietyName}`" :value="item.productionBatchId" />
              </template>
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">估产人</div>
            <el-select filterable clearable v-model="staffName" filterable clearable>
              <!-- 添加接口存的staffName和staffId没有关联(错误的对应关系也能存进去),所以在这里查询的时候只通过staffname查询 -->
              <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName"
                :value="item.staffName" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">估产日期</div>
            <el-date-picker v-model="predictDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" clearable />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="162" @add="add" @del="del" />
    </template>
    <template v-slot:categoryBox>
      <CategoryBar :categoryList="categoryList" @categoryBar="categoryBar" />
    </template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" class="margin-top10px" stripe
        border height="calc(100% - 50px)" :row-key="row => {
          return row.predictYieldId;
        }
          " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">
            {{ scoped.row.predictYieldId ? scoped.$index + 1 : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="pondName" label="鱼塘名称" />
        <el-table-column prop="batchName" label="批次编号" />
        <el-table-column prop="specificationName" label="估产百分比">
          <template v-if="scoped.row.minRate" slot-scope="scoped">
            {{ scoped.row.minRate + '% ~ ' + scoped.row.maxRate + '%' }}
          </template>
        </el-table-column>
        <el-table-column prop="predictWeight" label="估产重量（斤）" />
        <el-table-column min-width="120" prop="density" label="标准养殖密度" />
        <el-table-column min-width="120" prop="estimateDensity" label="实际养殖密度" />
        <el-table-column prop="staffName" label="估产人" />
        <el-table-column prop="predictDate" label="估产日期" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.predictYieldId" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="edit(scope.row)">修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :staff-list="staffList" :pond-list="pondList" :material-list="materialList"
        :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import CategoryBar from "@/components/CategoryBar";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new2.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from 'vuex';

import InPondEstimationHandingApi from "@/api/in-pond-estimation/in-pond-estimation-handing"
import BatchMangeApi from "@/api/batch-mange"
import MaterialApi from "@/api/basic-info-manage/material"
import FishPondApi from "@/api/basic-info-manage/fishPond"
import StaffApi from "@/api/basic-info-manage/staff";
export default {
  name: 'WaterChangeOperation',
  components: { ActionBar, Pagination, BaseMainSlot, AddEditDialog, CategoryBar },
  mixins: [mymixin],
  data () {
    return {
      productionBatchId: "",//批次ID
      fishPondId: "",//养殖鱼塘ID
      staffName: '',//经手人
      predictDate: "",//经手日期
      checkedIdList: [],
      pondList: [],//全部鱼塘
      batchList: [],//全部未完成批次
      FishVarietyData: [],//全部品种
      staffList: [],//全部员工/经手人
      materialList: [],//物料
      //分类列表
      categoryList: [
        { id: 1, mark: 'audit', name: "待审核", value: false },
        { id: 2, mark: 'audit', name: "已审核", value: true },
        { id: 3, mark: 'success', name: "审核通过", value: true },
        { id: 4, mark: 'success', name: "审核未通过", value: false },
      ],
      showAddEditDialog: false,
      // activeName: '1',//控制是否默认展开
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    this.queryPageList();
  },
  mounted () {
    this.queryFishPond()
    this.queryBatch()
    this.queryStaff()
    this.queryMaterial()
  },
  methods: {
    searchPage () {
      this.currentPage = 1
      this.queryPageList();
      setTimeout(() => {
        this.queryPageList();
      }, 200);
    },
    categoryBar (item) {
      this.audit = ''
      this.success = ''
      if (item.mark === 'audit') this.audit = item.value;
      if (item.mark === 'success') {
        this.audit = true
        this.success = item.value;
      }
      this.queryPageList()
    },
    //查询可选批次
    queryBatch () {
      BatchMangeApi.queryByCondition({}).then(res => {
        this.batchList = res.data;
      })
    },
    //查询可选物料
    queryMaterial () {
      MaterialApi.queryByCondition().then(res => {
        this.materialList = res.data;
      })
    },
    //查询可选鱼塘
    queryFishPond () {
      FishPondApi.queryByCondition().then(res => {
        this.pondList = res.data;
      })
    },
    //查询经手人
    queryStaff () {
      StaffApi.queryByCondition().then(res => {
        this.staffList = res.data;
      })
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {}
      }
      if (this.audit !== '') params.entity.audit = this.audit;
      if (this.success !== '') params.entity.success = this.success;
      if (this.productionBatchId) params.entity.productionBatchId = this.productionBatchId;
      if (this.fishPondId) params.entity.fishPondId = this.fishPondId;
      if (this.staffName) params.entity.staffName = this.staffName;
      if (this.predictDate) params.entity.predictDate = this.predictDate;
      InPondEstimationHandingApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true
      list.forEach(item => {
        if (!item.predictYieldId) {
          notvalue = false
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select (list) {
      let notvalue = true
      list.forEach(item => {
        if (!item.predictYieldId) {
          notvalue = false
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true
      let emptyList = []
      this.checkedIdList = list.map(item => {
        if (item.predictYieldId) {
          return item.predictYieldId
        } else {
          notvalue = false
          emptyList.push(item)
          return ""
        }
      });
      if (notvalue === false) {
        this.toggleNoSelection(emptyList)
        return
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 表格删除
    delmessage () {
      InPondEstimationHandingApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.queryPageList();
          this.$message.success("删除成功");
          this.resetActionBar();
        }
      });
    },
    // 单个删除
    delOne (row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      })
        .then(() => {
          InPondEstimationHandingApi.delById({ id: row.predictYieldId }).then(res => {
            if (res.success === true) {
              this.$message.success("删除成功!");
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 添加
    add () {
      this.nowChanceStau = 'add'
      this.titleText = "添加"
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true
    },
    // 修改
    edit (row) {
      if (row.predictYieldId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = 'edit'
        this.titleText = "修改"
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true
      } else {
        this.editObject = {}
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
